/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package orderProcessingProcess.server;

import java.util.Random;
import javax.jws.WebService;
import orderProcessingProcess.dataStorage.DataBackend;
import orderProcessingProcess.exceptions.faults.*;
import orderProcessingProcess.model.Customer;
import orderProcessingProcess.util.AIC_log;

/**
 * 
 * @author dreni
 */
@WebService(endpointInterface = "orderProcessingProcess.server.NotifyService", serviceName = "NotificationService", targetNamespace = "http://infosys.tuwien.ac.at/ait09/ass1/dto/notification", portName = "NotificationPT")
public class SMSServiceImpl implements NotifyService {
	
	private static final String type = "SMSService";
	
	public boolean notifyCustomer(Customer customer, String msg)
			throws UnknownCustomerFault, MessageTooLongFault {
		
		// check if method parameters are valid
		if (customer == null || msg == null || "".equals(msg)) {
			AIC_log.printerr(type, "Customer and/or Message are null.");
			return false;
		}

		// DataBackend, where we store Customer data etc...
		DataBackend db = DataBackend.getInstance();

		// Get Customer(checks if the customer exists)
		Customer cstm = db.getCustomer(customer);

		// check for the customer, if it exists in our datastorage
		if (cstm == null) {
			AIC_log.printerr(type, "Unknown Customer.");
			throw new UnknownCustomerFault(
					"Customer doesn't exist in our Database.");
		}

		// check the length of the message, shouldn't be longer then 160
		// characters
		if (msg.length() > 160) {
			AIC_log.printerr(type, "Message is longer than 160 Characters and can't be send!");
			throw new MessageTooLongFault(
					"Message is longer than 160 Characters and can't be send!");
		}

		// here comes the part where it says "..., and randomly return true or
		// false
		// return randomly true/false
		boolean returnStatus = true;

		Random randomInt = new Random();
		if (randomInt.nextInt(2) == 1) {
			returnStatus = false;
		}

		AIC_log.print(type, cstm.getName());
		AIC_log.print(type, "Sending message :: " + msg);
		AIC_log.print(type, "Success :: " + returnStatus);
		
		return returnStatus;
	}
}
